﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Honestysoft.Financing.Model;
using Honestysoft.Baseframework.WebUI;
using Honestysoft.BaseObject;
using Honestysoft.BaseLibrary.ExceptionHandling;

namespace Honestysoft.Financing.WebUI.Module.Reports
{
    public partial class WeekReport : BasePage
    {
        protected override void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindYear();
                BindWeek(Convert.ToInt32(ddlTheYear.SelectedValue));
                ddlWeek.SelectedValue = Honestysoft.BaseLibrary.Common.DateHelper.GetWeekOnYear(DateTime.Today).ToString();
                BindReport();
            }
        }
        /// <summary>
        /// 绑定自2011至今日的年度
        /// </summary>
        private void BindYear()
        {
            int year = 2011;
            while (year <= DateTime.Today.Year)
            {
                ddlTheYear.Items.Add(new ListItem(year.ToString(), year.ToString()));
                year++;
            }
            ddlTheYear.SelectedValue = DateTime.Today.Year.ToString();
        }

        private void BindWeek(int year)
        {
            ddlWeek.Items.Clear();
            int iLastWeekNumber = Honestysoft.BaseLibrary.Common.DateHelper.GetWeekOfYear(year);      //本年最后是多少周 
            for (int iNum = 1; iNum <= iLastWeekNumber; iNum++)
            {
                ListItem li = new ListItem(iNum.ToString(), iNum.ToString());
                ddlWeek.Items.Add(li);
            }
        }
        private void BindReport()
        {
            Honestysoft.Financing.BusinessLogic.Reports bll = new BusinessLogic.Reports(Globals.CurrentConnStringName);
            int year=Convert.ToInt32(ddlTheYear.SelectedValue);
            int week =Convert.ToInt32(ddlWeek.SelectedValue);
            Bu_WeekReport report = bll.GetWeekReport(year, week);
            if (report == null)
            {
                report = bll.CreateWeekReport(year, week,UserLoginer.LoginUser.ID,UserLoginer.LoginUser.Name);
            }
            hidID.Value = report.ID;
            dtpEndDate.Text = report.EndDate.ToString("yyyy-MM-dd");
            dtpSendDate.Text = report.SendDate.ToString("yyyy-MM-dd");
            dtpStartDate.Text = report.StartDate.ToString("yyyy-MM-dd");
            rptList.DataSource = report.Others;
            rptList.DataBind();
            btnAdd.Attributes.Add("onclick", "return AddOther('" + report.ID + "');");
            Page.ClientScript.RegisterStartupScript(typeof(string), "queryReport", "showReport();", true);
        }

        protected void ddlTheYear_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindWeek(Convert.ToInt32(ddlTheYear.SelectedValue));
            ddlWeek.SelectedIndex = 0;
            BindReport();
        }

        protected void ddlWeek_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindReport();
        }

        protected void btnCurWeek_Click(object sender, EventArgs e)
        {
            ddlTheYear.SelectedValue = DateTime.Today.Year.ToString();
            ddlWeek.SelectedValue = Honestysoft.BaseLibrary.Common.DateHelper.GetWeekOnYear(DateTime.Today).ToString();
            BindReport();
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                DateTime startDate = Convert.ToDateTime(dtpStartDate.Text);
                DateTime endDate = Convert.ToDateTime(dtpEndDate.Text);
                DateTime sendDate = Convert.ToDateTime(dtpSendDate.Text);
                new Honestysoft.Financing.BusinessLogic.Reports(Globals.CurrentConnStringName).SaveWeekReport(hidID.Value,
                  startDate, endDate, sendDate);
                ScriptHelper.Window.Alert("保存成功！");
            }
            catch (Exception ex)
            {
                Honestysoft.BaseLibrary.ExceptionHandling.ExceptionHelper.ExceptionDistribute(ex, false);
                ScriptHelper.Window.Alert("保存失败！\r\n" + ex.Message);
            }
        }
        /// <summary>
        /// 得到表格行操作按钮脚本
        /// </summary>
        /// <param name="dataId">记录ID</param>
        /// <returns>操作按钮脚本</returns>
        protected string GetButtonScript(string dataId)
        {
            string script = "";
            script += ScriptHelper.LinkButton.GetGridImageButton("EditOther('" + dataId + "')", ScriptHelper.Image.GetUrl(ScriptHelper.Image.ImageType.Edit), "修改周报其他事项", "");
            script += "&nbsp;";
            script += ScriptHelper.LinkButton.GetGridDeleteButton(dataId, btnDelete.ClientID);
            return script;
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            BindReport();
        }

        /// <summary>
        /// 得到选中的记录ID集合
        /// </summary>
        /// <returns></returns>
        private string[] GetSelectedRowKeys()
        {
            string strId = Request["chkID"] + "";
            if (string.IsNullOrEmpty(strId)) { return null; }
            return strId.Split(',');
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                string[] arrId = GetSelectedRowKeys();
                BaseBusinessLogic<Bu_WeekReport_Other> baseLogic = new BaseBusinessLogic<Bu_WeekReport_Other>();
                baseLogic.DeleteByKey(arrId);
                ScriptHelper.Window.Alert("删除成功！");
                BindReport();
            }
            catch (Honestysoft.BaseLibrary.ExceptionHandling.BaseException bex)
            {
                ExceptionHelper.ExceptionDistribute(bex, false);
                ScriptHelper.Window.Alert(bex.Message);
            }
            catch (Exception ex)
            {
                ExceptionHelper.ExceptionDistribute(ex, false);
                ScriptHelper.Window.Alert("删除失败！系统发生了未知错误！");
            }
        }

        protected void btnRebind_Click(object sender, EventArgs e)
        {
            BindReport();
        }
    }
}